//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;


namespace OpenLS.Chart.UI
{
    partial class Bar3DChartResources
    {
        public Bar3DChartResources()
        {
            InitializeComponent();
        }



        private void OnFormat3DRotation(object sender, ExecutedRoutedEventArgs e)
        {

            FrameworkElement o = (FrameworkElement)sender;
            ChartArea a = ChartElement.GetChartArea(o);
            PropertyHelper.ShowPropertiesFor(a, o, ChartCommands.Format3DRotation.Text, PropertyTabHint.View3D);
            e.Handled = true;
        }
       
        private void Always(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

        private void Panel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ((IInputElement) sender).Focus();
            e.Handled = true;
        }

        private void OnFormatDataSeries(object sender, ExecutedRoutedEventArgs args)
        {
            FrameworkElement e = (FrameworkElement)args.Source;
            PropertyHelper.ShowPropertiesFor(ChartElement.GetSelectedObject( e), e, ChartCommands.FormatDataSeries.Text, PropertyTabHint.None);
            args.Handled = true;
        }

        private void vp_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Viewport3D viewport3D = (Viewport3D) sender;
            Point mouseposition = e.GetPosition(viewport3D);
            PointHitTestParameters pointparams = new PointHitTestParameters(mouseposition);
            FrameworkElement owner = ChartElement.GetChartAreaOwner(viewport3D);
            ChartElement.SetSelectedObject(viewport3D, null);

            VisualTreeHelper.HitTest(viewport3D,
                null,
                delegate(HitTestResult rawresult) { return HTResult(owner, rawresult); }, 
                pointparams);
        }
        private HitTestResultBehavior HTResult(DependencyObject owner, HitTestResult rawresult)
        {
            RayHitTestResult rayResult = rawresult as RayHitTestResult;
            if (rayResult == null)
                return HitTestResultBehavior.Continue;

            if (rayResult != null)
            {
                RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;
                if (rayMeshResult != null)
                {
                    Model3D hitgeo = rayMeshResult.ModelHit;
                    if (hitgeo != null)
                    {
                        Debug.WriteLine(hitgeo);
                        Debug.WriteLine(hitgeo.GetHashCode());
                        object s = ChartElement.GetObjectContext(hitgeo);
                        if (s != null)
                            ChartElement.SetSelectedObject(owner, s);
                        Debug.WriteLine(ChartElement.GetObjectContext(hitgeo));
                    }
                }
            }

            return HitTestResultBehavior.Continue;
        }

        private void OnFormatFloor(object sender, ExecutedRoutedEventArgs e)
        {
            FrameworkElement owner = (FrameworkElement)sender;
            Surface surface = ChartElement.GetSelectedObject(owner) as Surface;
            RoutedUICommand cmd = ChartCommands.FormatFloor;
            if (surface == null)
            {
                ChartArea area = ChartElement.GetChartArea(owner);
                if (area.Floor == null)
                {
                    area.Floor = new Surface();
                    surface = area.Floor;
                }
            }
            PropertyHelper.ShowPropertiesFor(surface, owner, cmd.Text, PropertyTabHint.None);
            e.Handled = true;

        }

        private void OnFormatWalls(object sender, ExecutedRoutedEventArgs e)
        {
            FrameworkElement owner = (FrameworkElement)sender;
            Surface surface = ChartElement.GetSelectedObject(owner) as Surface;
            if (surface == null)
            {
                ChartArea area = ChartElement.GetChartArea(owner);
                if (area.SideWall == null)
                {
                    area.SideWall = new Surface();
                    area.BackWall = new Surface();
                    surface = area.SideWall;
                }
            }
            RoutedUICommand cmd = ChartCommands.FormatWalls;
            PropertyHelper.ShowPropertiesFor(surface, owner, cmd.Text, PropertyTabHint.None);
            e.Handled = true;

        }



    }
}
